﻿using Engine;
using TemplatesDatabase;

namespace Game
{
    // 起始门块行为，继承自块行为子系统
    public class StartDoorBlockBehavior : SubsystemBlockBehavior
    {
        // 天空岛地形子系统引用
        public SubsystemSkyIslandTerrain subsystemTerrain;

        // 树苗块行为子系统引用
        public SubsystemSaplingBlockBehavior subsystemSaplingBlockBehavior;

        // 处理的块类型数组，仅处理块 ID 为 807 的块
        public override int[] HandledBlocks => new int[1] { 807 };

        // 与方块交互时触发的事件
        public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            int x = raycastResult.CellFace.X;
            int y = raycastResult.CellFace.Y;
            int z = raycastResult.CellFace.Z;
            Vector3 vector = default(Vector3);
            bool flag = false;
            int num = -1000;
            // 循环尝试寻找合适的生成位置
            while (true)
            {
                int num2;
                int num3;
                if (num < 1000)
                {
                    for (num2 = 1000; num2 > -1000; num2--)
                    {
                        num3 = 66;
                        while (num3 > 58)
                        {
                            int cellContentsFast = subsystemTerrain.Terrain.GetCellContentsFast(x + num + 1, num3, z + num2);
                            int cellContentsFast2 = subsystemTerrain.Terrain.GetCellContentsFast(x + num - 1, num3, z + num2);
                            int cellContentsFast3 = subsystemTerrain.Terrain.GetCellContentsFast(x + num, num3, z + num2 + 1);
                            int cellContentsFast4 = subsystemTerrain.Terrain.GetCellContentsFast(x + num, num3, z + num2 - 1);
                            // 如果周围方块不是空气，则继续寻找下一个位置
                            if (cellContentsFast != 8 || cellContentsFast2 != 8 || cellContentsFast3 != 8 || cellContentsFast4 != 8)
                            {
                                num3--;
                                continue;
                            }

                            // 找到合适的位置，跳出循环
                            goto IL_00ec;
                        }
                    }

                    num++;
                    continue;
                }

                // 如果没有找到合适的位置，则提示玩家提高视距
                if (!flag)
                {
                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("提高视距，再试试运气吧", Color.Yellow, blinking: true, playNotificationSound: true);
                }

                break;
            IL_00ec:
                vector = new Vector3(x + num, num3, z + num2);
                flag = true;
                Random random = new Random();
                // 随机选择一种树的笔刷并在指定位置绘制
                ReadOnlyList<TerrainBrush> treeBrushes = PlantsManager.GetTreeBrushes(TreeType.Birch);
                TerrainBrush terrainBrush = treeBrushes[random.Int(0, treeBrushes.Count - 1)];
                terrainBrush.Paint(base.SubsystemTerrain, x + num + 2, num3 + 2, z + num2 + 1);
                // 在生成的地方设置湿度和温度
                for (int i = 0; i < 3; i++)
                {
                    subsystemTerrain.Terrain.SetHumidity((int)vector.X + i, (int)vector.Z, 12);
                    subsystemTerrain.Terrain.SetTemperature((int)vector.X + i, (int)vector.Z, 10);
                    subsystemTerrain.Terrain.SetHumidity((int)vector.X + i, (int)vector.Z + i, 12);
                    subsystemTerrain.Terrain.SetTemperature((int)vector.X + i, (int)vector.Z + i, 10);
                }
                // 将物品添加到玩家的背包中
                IInventory inventory = componentMiner.Inventory;
                inventory.AddSlotItems(3, 108, 5);
                inventory.AddSlotItems(4, 256, 5);
                inventory.AddSlotItems(5, 225, 5);
                inventory.AddSlotItems(6, 14, 5);
                // 将玩家位置设置到生成位置
                componentMiner.ComponentPlayer.ComponentBody.Position = vector;
                // 显示提示消息给玩家
                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("空岛地图！", Color.Green, blinking: true, playNotificationSound: true);
                break;
            }

            return true;
        }

        // 加载方法重写
        public override void Load(ValuesDictionary valuesDictionary)
        {
            base.Load(valuesDictionary);
            // 查找并设置天空岛地形子系统和树苗块行为子系统
            subsystemTerrain = base.Project.FindSubsystem<SubsystemSkyIslandTerrain>(throwOnError: true);
            subsystemSaplingBlockBehavior = base.Project.FindSubsystem<SubsystemSaplingBlockBehavior>(throwOnError: true);
        }
    }
}
